-- UIShipSkillUpgrade
-- Created by chengb Jun/6/2015
-- 幽灵船技能升级

UIShipSkillUpgrade = class("UIShipSkillUpgrade", function()
    return cc.Layer:create();
end);

function UIShipSkillUpgrade.create(skillId)
    return UIShipSkillUpgrade.new(skillId);
end

function UIShipSkillUpgrade:ctor(skillId)
    -- 初始化
    self:setName("UIShipSkillUpgrade");
    local node = cc.CSLoader:createNode("layout/bine/BineSkillLearn.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.skillId = skillId;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIShipSkillUpgrade:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIShipSkillUpgrade:registerEventCallback()
    -- 关注幽灵船升级技能事件
    EventMgr.register("UIShipSkillUpgrade", event.UPGRADE_SHIP_SKILL, function(args)
        local function callback()
            -- 重绘界面
            local uiShipMain = UIMgr.getCurrentScene():getFormByName("UIShipMain");
            uiShipMain:redraw();
        end

        -- 升级技能音效
        AudioM.playFx("skill_learn");

        -- 播放动画
        local uiShipMain = UIMgr.getCurrentScene():getFormByName("UIShipMain");
        local child = uiShipMain:getSkillChild(self.skillId);
        local iconImg = child:getChildByName("icon");
        local level = SkillM.query(self.skillId, "level");
        local starImg = findChildByName(child, "star_node/layer/star" .. level);
        playBineSkillUpgradeEffect(iconImg, starImg, callback);

        -- 关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIShipSkillUpgrade");
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIShipSkillUpgrade 界面析构清理");

            -- 移除所有事件关注
            EventMgr.removeAll("UIShipSkillUpgrade");
        end
    end);
end

-- 注册点击事件
function UIShipSkillUpgrade:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIShipSkillUpgrade");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            -- 尝试升级
            self:tryUpgradeSkill();
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_upgrade"));
end

-- 重绘
function UIShipSkillUpgrade:redraw()
    -- 技能名称
    local nameLabel = findChildByName(self.node, "CT/name");
    local skillName = SkillM.query(self.skillId, "name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(skillName);

    -- 技能星级
    local starNode = findChildByName(self.node, "CT/star_node");
    local level = SkillM.query(self.skillId, "level");
    local layer = createStarLayer(level, "images/ui/symbol/career_star_01.png", "right");
    if layer then
        starNode:addChild(layer);
    end

    local desc = "";

    -- 消耗的物品描述
    local baseSkill = SkillM.findBaseSkill(self.skillId);
    local cost = GhostShipM.calcUpgradeSkillCost(baseSkill);
    local classId = table.keys(cost)[1];
    local itemDesc = ItemM.getItemDesc(classId);

    if itemDesc then
        desc = desc .. itemDesc .. "\n\n";
    end

    -- 学习提示
    desc = desc .. getLocStr("ship_skill_tip2") .. "\n";

    -- 施法方式
    local castType = SkillM.query(self.skillId, "type");
    if castType == USER_SKILL then
        -- 主动
        local cd = GhostShipM.getSkillCD(ME.user.ship, self.skillId);
        local castDesc = string.format(getLocStr("ship_skill_initiative_cast"), cd);
        desc = desc .. castDesc .. "\n";
    end

    -- 技能描述
    local skillDesc = SkillM.getSkillDesc(self.skillId);
    local skillDesc = "[blue]" .. extractString(skillDesc) .. "[-]";
    desc = desc .. skillDesc;

    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    -- 重绘升级按钮
    self:redrawButton();

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, { nameLabel, descLabel, btnOk});

    -- 星级节点与名称在同一行
    starNode:setPositionY(nameLabel:getPositionY());
end

-- 重绘升级按钮
function UIShipSkillUpgrade:redrawButton()
    local btnOk = findChildByName(self.node, "CT/btn_ok");

    local curLevel = SkillM.query(self.skillId, "level");
    local maxLevel = SkillM.getSkillMaxLevel(self.skillId);
    if curLevel >= maxLevel then
        -- 技能已满级，则升级按钮隐藏
        btnOk:setVisible(false);
    else
        btnOk:setVisible(true);
        btnOk:setTouchEnabled(true);
        btnOk:setBright(true);
    end
end

-- 尝试升级技能
function UIShipSkillUpgrade:tryUpgradeSkill()
    local ret = GhostShipM.canUpgradeSkill(self.skillId);
    if ret ~= true then
        if ret == "cost_lack" then
            -- 消耗不足
            local cost    = GhostShipM.calcUpgradeSkillCost(self.skillId);
            local classId = table.keys(cost)[1];
            local name    = ItemM.query(classId, "name");

            alert(string.format(getLocStr("item_not_enough"), name));
        end

        return;
    end

    -- 升级技能
    DungeonActionM.go("upgrade_ship_skill", self.skillId);
end
